package com.sx.zongzhi.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.KeyPunishSearchBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 重点地区搜索
 */
public class KeyPunishSearchActivity extends BaseActivity implements IPublicBeanView {

    @BindView(R.id.tv_PunishOrder) EditText et_PunishOrder;
    @BindView(R.id.sp_OrderProblem) Spinner sp_OrderProblem;
    @BindView(R.id.sp_InvolveType) Spinner sp_InvolveType;
    @BindView(R.id.tv_InvolveRange) EditText et_InvolveRange;
    @BindView(R.id.tv_LeaderComp) EditText et_LeaderComp;
    @BindView(R.id.tv_JoinComp) EditText et_JoinComp;
    @BindView(R.id.tv_PunishLeader) EditText et_PunishLeader;
    @BindView(R.id.tv_leaderTel) EditText et_leaderTel;
    @BindView(R.id.tv_LeaderPhone) EditText et_LeaderPhone;
    @BindView(R.id.sp_Evaluate) Spinner sp_Evaluate;

    private List<GlobalBean> problemList=new ArrayList<>();
    private List<GlobalBean> involveList=new ArrayList<>();
    private List<GlobalBean> evaluateList=new ArrayList<>();


    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_key_punish_search);
        ButterKnife.bind(this);
        setTitleRight(R.string.submit);
        setTitleName("排查整治查询");
        initData();
    }

    private void initData() {
        PublicListPest publicListPest=new PublicListPest(this);
        publicListPest.getPublicList("涉及区域类型");
        publicListPest.getPublicList("突出治安问题");
        publicListPest.getPublicList("效果评估");
    }


    @OnClick({R.id.tv_right})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right://提交
                submit();
                break;
        }
    }

    private void submit() {

        KeyPunishSearchBean searchBean=new KeyPunishSearchBean();
        searchBean.setName(et_PunishOrder.getText().toString().trim());
        String problemId="";
        GlobalBean obBean = (GlobalBean) sp_OrderProblem.getSelectedItem();
        if (obBean!=null){
            problemId=(obBean.getId()).equals("-1") ?"":obBean.getId();
        }
        searchBean.setProblemsId(problemId);
        String involveId="";
        GlobalBean itBean = (GlobalBean) sp_InvolveType.getSelectedItem();
        if (itBean!=null){
            involveId=(itBean.getId()).equals("-1") ?"":itBean.getId();
        }
        searchBean.setTypeId(involveId);
        searchBean.setRelateArea(et_InvolveRange.getText().toString().trim());
        searchBean.setRepairLeaderUnit(et_LeaderComp.getText().toString().trim());
        searchBean.setRepairPartakeUnit(et_JoinComp.getText().toString().trim());
        searchBean.setLeaderName(et_PunishLeader.getText().toString().trim());
        searchBean.setLeaderTelephone(et_leaderTel.getText().toString().trim());
        searchBean.setLeaderPhone(et_LeaderPhone.getText().toString().trim());
        String evaluateID="";
        GlobalBean eBean = (GlobalBean) sp_Evaluate.getSelectedItem();
        if (eBean!=null){
            evaluateID=(eBean.getId()).equals("-1") ?"":eBean.getId();
        }
        searchBean.setEvaluateId(evaluateID);
        MyRxBus.get().post(new MsgEvent<>("KeyPunishActivity",2,searchBean));
        finish();

    }


    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }



    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("效果评估".equals(publicBean.getDomainName())){
            evaluateList=publicBean.getDicts();
            evaluateList.add(0,new GlobalBean("未选择","-1"));
            if (evaluateList!=null){
                SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,evaluateList);
                sp_Evaluate.setAdapter(adapter);

            }
        }
        if ("涉及区域类型".equals(publicBean.getDomainName())){
            involveList=publicBean.getDicts();
            involveList.add(0,new GlobalBean("未选择","-1"));
            if (involveList!=null){
                SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,involveList);
                sp_InvolveType.setAdapter(adapter);

            }
        }
        if ("突出治安问题".equals(publicBean.getDomainName())){
            problemList=publicBean.getDicts();
            problemList.add(0,new GlobalBean("未选择","-1"));
            if (problemList!=null){
                SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,problemList);
                sp_OrderProblem.setAdapter(adapter);
            }
        }

    }

}
